Ext.define('Ext.app.TaoMoiKhoWindow', {
    extend: 'Ext.window.Window',
    alias: 'widget.taoMoiKhoWindow',
    height: 210,
    width: 400,
    layout: 'fit',
    plain:true,
    items: {
        xtype: 'form',
        layout: 'absolute',
        defaultType: 'textfield',
        border: false,

        items: [{
            fieldLabel: this.translations.get("HOME.TEN_KHO"),
            fieldWidth: 60,
            x: 5,
            y: 5,
            name: 'name',
            allowBlank: false,
            blankText: this.translations.get("HOME.BLANK_TEXT"),
            anchor: '-5'  // anchor width by percentage
        }, {
            fieldLabel: this.translations.get("HOME.DIA_CHI"),
            fieldWidth: 60,
            x: 5,
            y: 35,
            name: 'address',
            allowBlank: false,
            blankText: this.translations.get("HOME.BLANK_TEXT"),
            anchor: '-5'  // anchor width by percentage
        }, {
            fieldLabel: this.translations.get("HOME.PHONE"),
            fieldWidth: 60,
            x: 5,
            y: 65,
            name: 'phone',
            anchor: '-5'  // anchor width by percentage
        }, {
            fieldLabel: this.translations.get("HOME.PHAN_LOAI"),
            fieldWidth: 60,
            x: 5,
            y: 95,
            name: 'type',
            value: 'CHINH_PHAM',
            allowBlank: false,
            blankText: this.translations.get("HOME.BLANK_TEXT"),
            xtype: 'combobox',
            valueField: 'code',
            displayField: 'name',
            queryMode: 'local',
            store: {
                autoDestroy: true,
                fields: ['code', 'name'],
                data: [
                    {code: 'CHINH_PHAM', name: this.translations.get("HOME.CHINH_PHAM")},
                    {code: 'THU_PHAM', name: this.translations.get("HOME.THU_PHAM")},
                    {code: 'PHE_PHAM', name: this.translations.get("HOME.PHE_PHAM")}
                ]
            },
            editable: false,
            anchor: '-5'  // anchor width by percentage
        }]
    },

    buttons: [{
        text: 'Ok',
        handler: function(button) {
            var wind = button.up('taoMoiKhoWindow');
            var form = wind.down('form');
            if(!form.isValid()) {
                return;
            }
            var params = {
                'name': form.down('[name=name]').getValue(),
                'address': form.down('[name=address]').getValue(),
                'phone': form.down('[name=phone]').getValue(),
                'type': form.down('[name=type]').getValue()
            };
            wind.mask(translations.get("HOME.PLEASE_WAIT"));
            Ext.Ajax.request({
                url: 'TaoMoiKho.jsp',
                params: params,
                method: 'POST',
                success: function(response, opts) {
                    wind.unmask();
                    var obj = Ext.decode(response.responseText);
                    if(!obj.success) {
                        showErrorMsg(obj.msg);
                    }
                    else {
                        wind.opener.reload();
                        wind.close();
                    }
                },
                failure: function(response, opts) {
                    wind.unmask();
                    alert('server-side failure with status code ' + response.status);
                }
            });
        }
    },{
        text: 'Cancel', 
        handler: function(button) {
            var wind = button.up('taoMoiKhoWindow');
            if(wind != null) {
                wind.close();
            }
        }
    }],
    
    listeners: {
        beforeclose: function () {
            Ext.getBody().unmask();
        }
    },

    initComponent : function() {
        Ext.getBody().mask();
        
        this.callParent();
    },
    
    setOpener: function(opener) {
        this.opener = opener;
    }
});
